package com.example.firstapp.storeData

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.firstapp.R
import com.example.firstapp.databinding.ActivityStoreData1Binding
import com.example.firstapp.databinding.ActivityStoreData2Binding
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStreamWriter

class storeData2SharedPreferences : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_store_data2)

        val binding = ActivityStoreData2Binding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.storeData2Submit.setOnClickListener {
            val input = binding.storeData2Input.text.toString()
            save(input)
        }
        binding.storeData2Get.setOnClickListener {
            binding.storeData2Show.text = show()
        }
    }
    private fun save(input:String){
        val editor = getSharedPreferences("data2", Context.MODE_PRIVATE).edit()
        editor.putString("input",input)
        editor.apply()  // 提交
    }
    private fun show():String{
        val prefs = getSharedPreferences("data2", Context.MODE_PRIVATE)
        val output = prefs.getString("input", "")
        return output.toString()
    }
}